<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 订单状态管理
 * @author zxj
 *
 */
class Product_status extends MY_Controller {
	private $t_product_status = 'mt_product_status';
	function __construct() {
		parent::__construct();
	}


	/**
	 * 列表页
	 */
	function index()
    {
        //获取表单数据
        $data = $formField = gp();
        //查询条件
        $where = $whereLike = array();
        //默认select设置
        $keyword = trim($data['keyword']);
        $searchStr = 'orders/Product_status/index/?';
        if($keyword){
            if (preg_match('/[\x7f-\xff]/', $keyword)) {
                $whereLike['name_ch'] = $keyword;
            }else{
                $whereLike['name_en'] = $keyword;
            }
            $searchStr .= 'keyword='.$keyword;
        }
        //分页
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $config['base_url'] = blang_url($searchStr);
        $config['total_rows'] = $this->common_model->countRows($this->t_product_status, $where, '', $whereLike);
        $config['per_page'] = $data['outputExcel'] ? $config['total_rows'] : 20;
        $page = max(1, intval($data['page']));
        $config['uri_segment'] = config_item('enable_lang') ? 5 : 4;
        $slimit = ($page - 1) * $config['per_page'];
        $this->pagination->initialize($config);
        $dataList = $this->common_model->select($this->t_product_status, $slimit, $config['per_page'], 'id asc', $where, '', $whereLike);
        $data['show_pages'] = $this->pagination->create_links();
        //重构数据
        foreach ($dataList as $key => &$rs) {
            if($rs['available']==1){
                $rs['availableName'] = '可用';
            }elseif($rs['available']==2){
                $rs['availableName'] = '已禁用';
            }else{
                $rs['availableName'] = '已删除';
            }
            $sTypeFirst = $this->common_model->selectFirst('mt_service_type',array('id'=>$rs['typeid']));
            $rs['sTypeName'] = $sTypeFirst['name_ch'];
            $rs['add_time'] = date('Y-m-d H:i:s',$rs['add_time']);
        }
        unset($rs);
        $data['dataList'] = $dataList;
        //导出数据
        $data['outputUrl'] = blang_url($searchStr . '&outputExcel=true');
        if ($data['outputExcel']) {
            $fieldData = array();
            excel_output(date('y-m-d'), $fieldData, $dataList);
        } else {
            //视图以及样式
            $this->load->view('defaults/product_status_list', $data);
            $this->load->view('defaults/common/footer');
        }
    }


    /**
     * 新增
     */
    function add()
    {
        //表单处理
        $data['post_type'] = __FUNCTION__;
        //接收数据
        $formData = gp();
        if ($formData['submits']) {
            $inData = array();
            $formData['add_user'] = $this->users['account'];
            $formData['add_time'] = time();
            $fieldsList = $this->db->list_fields($this->t_product_status);
            foreach ($formData as $key => $val) {
                if (($val || $val === '0') && in_array($key, $fieldsList)) {
                    $inData[$key] = $val;
                }
            }
            $sql = 'select * from '.$this->t_product_status.' where (name_ch = \''.$inData['name_ch'].'\' or name_en = \''.$inData['name_en'].'\') and typeid='.$inData['typeid'].' ';
            $existsData = $this->common_model->getQuery($sql,'row_array');
            if(!$existsData){
                $this->common_model->insert($this->t_product_status, $inData);
            }
            //跳转
            redirect('orders/Product_status/');
        } else {
            $sTypeList = $this->common_model->selectCustom('mt_service_type');
            $data['sTypeList'] = $sTypeList;
            //views
            $this->breadcrumb->append_crumb(
                array('url' => 'orders/Product_status/', 'name' => '返回当前列表')
            );
            $this->load->view('defaults/product_status_post', $data);
            $this->load->view('defaults/common/footer');
        }
    }



    /**
     * 修改状态
     * @param string $id
     */
    function edit($id = '')
    {
        //表单处理
        $data['post_type'] = __FUNCTION__;
        //接收数据
        $id = intval($id) ? intval($id) : gp('id');
        $data['id'] = $id;
        $statusFirst = $this->common_model->selectFirst($this->t_product_status, array('id' => $id));
        if (!$statusFirst) {
            show_error('this status not found');
        }
        //判断是否修改数据
        $formData = gp();
        if ($formData['submits']) {
            $inData = array();
            $formData['add_user'] = $this->users['account'];
            $formData['add_time'] = time();
            foreach ($formData as $key => $val) {
                if (array_key_exists($key, $statusFirst) && $statusFirst[$key] != $val) {
                    $inData[$key] = $val;
                }
            }
            if ($inData) {
                $this->common_model->update($this->t_product_status, $inData, array('id' => $id));
            }
            //跳转
            redirect('orders/Product_status/');
        } else {
            $sTypeList = $this->common_model->selectCustom('mt_service_type');
            $data['sTypeList'] = $sTypeList;
            $data['statusFirst'] = $statusFirst;
            //default
            $ckdAvailable[$statusFirst['available']] = 'checked';
            $data['ckdAvailable'] = $ckdAvailable;
            $optStypeid[$statusFirst['typeid']] = 'selected';
            $data['optStypeid'] = $optStypeid;
            //views
            $this->breadcrumb->append_crumb(
                array('url' => 'orders/Product_status/', 'name' => '返回当前列表')
            );
            $this->load->view('defaults/product_status_post', $data);
            $this->load->view('defaults/common/footer');
        }
    }



    /**
     * 删除状态
     * @param string $id
     * @param string $page
     */
    function del($id = '',$page = ''){
        if($this->users['account'] != 'zlj')
            show_error('管理员的权限');
        $id = intval($id);
        $where = array('id'=>$id);
        $this->common_model->delete($this->t_product_status,$where);
        redirect(blang_url('orders/Product_status/index/?page='.intval($page)));
    }




}
